package com.ithema.notebook.dialog;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.EditorTextField;
import com.ithema.notebook.data.DataCenter;
import com.ithema.notebook.data.DataConvetor;
import com.ithema.notebook.data.NoteData;
import com.ithema.notebook.notify.NotifyType;
import com.ithema.notebook.notify.SystemNotify;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * Package: com.ithema.notebook.dialog
 * Description：
 *
 * @author: huangjian
 * Date:  2020/10/18 16:07
 * Modified By:
 */
public class AddNoteDialog extends DialogWrapper {
    private EditorTextField tfTitle;
    private EditorTextField tfMark;

    public AddNoteDialog() {
        super(true);
        setTitle("添加笔记");
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel jPanel = new JPanel(new BorderLayout());
        tfTitle = new EditorTextField("笔记标题");
        tfMark = new EditorTextField("笔记内容");
        tfMark.setPreferredSize(new Dimension(200, 100));
        jPanel.add(tfTitle, BorderLayout.NORTH);
        jPanel.add(tfMark, BorderLayout.CENTER);
        return jPanel;
    }

    @Override
    protected JComponent createSouthPanel() {
        JPanel jPanel = new JPanel();
        JButton jb_create = new JButton("添加笔记列表");
        jb_create.addActionListener(e -> {
            String titleText = tfTitle.getText();
            String markText = tfMark.getText();
            String fileType = DataCenter.FILE_NAME.substring(DataCenter.FILE_NAME.lastIndexOf(".") + 1);
            NoteData noteData = new NoteData(titleText,
                    markText,
                    DataCenter.SELECT_TEXT,
                    DataCenter.FILE_NAME,
                    fileType);
            DataCenter.NOTE_LIST.add(noteData);
            DataCenter.DEFAULT_TABLE_MODEL.addRow(DataConvetor.convert(noteData));
            SystemNotify.notify(NotifyType.ADD_NOTEBOOK_SUCCESS);
            AddNoteDialog.this.dispose();
        });
        jPanel.add(jb_create);
        return jPanel;
    }
}
